package br.com.salao.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.salao.bean.DateBusy;
import br.com.salao.dao.DateBusyDao;
import br.com.salao.util.JpaUtil;

public class DateBusyDaoImpl implements DateBusyDao {
	
	private final GenericDao<DateBusy> dao;
	
	public DateBusyDaoImpl(EntityManager em){
		dao = new GenericDao<DateBusy>(em, DateBusy.class);
	}
	
	public DateBusy inserir(DateBusy dateBusy){
		return dao.salvar(dateBusy);		
	}

	@SuppressWarnings("unchecked")
	public List<DateBusy> buscarDateBusyPorEstabelecimento(int idEstabelecimento){
		

		EntityManager em =  JpaUtil.getEntityManager();
		List<DateBusy> datasFechadas = null;		

		try {
			
			Query query = em.createQuery("from DateBusy d where d.estabelecimento.id = :idEstabelecimento");
			query.setParameter("idEstabelecimento", idEstabelecimento);
			datasFechadas = (List<DateBusy>)query.getResultList();
			
		} catch (Exception e) {
			e.printStackTrace();
			datasFechadas = null;

		} finally {
			if(em != null)
				em.close();
		}
		return datasFechadas;		
	}
	
	@SuppressWarnings("unchecked")
	public List<DateBusy> buscarDateBusyPorEstabelecimentoEData(int idEstabelecimento, Date dataFechada){
		
	
		EntityManager em = JpaUtil.getEntityManager();
		List<DateBusy> datasFechadas = null;		

		try {
			
			Query query = em.createQuery("from DateBusy d where d.estabelecimento.id = :idEstabelecimento and d.dateBusy = :data");
			query.setParameter("idEstabelecimento", idEstabelecimento);
			query.setParameter("data", dataFechada);
			
			datasFechadas = (List<DateBusy>)query.getResultList();
			
		} catch (Exception e) {
			e.printStackTrace();
			datasFechadas = null;

		} finally {
			if(em != null)
				em.close();
		}
		return datasFechadas;		
	}
}
